package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.NewsDDAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.NewsDDEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class NewsDDListActivity extends BaseActivity {
    private String type;
    private String typeName;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private NewsDDAdapter newsDDAdapter;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.ll_noRecord)
    LinearLayout ll_noRecord;
    @BindView(R.id.ll_title)
    LinearLayout ll_title;
    @BindView(R.id.tv_detail)
    TextView tv_detail;
    private List<NewsDDEntity> list = new ArrayList<>();

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_ddlist;
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        list.clear();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        if (newsDDAdapter == null) {
            newsDDAdapter = new NewsDDAdapter(this, list, "0");
        }
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(newsDDAdapter);
        newsDDAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(NewsDDListActivity.this, NewsDDDetailActivity.class)
                        .putExtra("type", type)
                        .putExtra("imgUrl", list.get(position).getImgUrl())
                        .putExtra("nikeName", list.get(position).getNikeName())
                        .putExtra("id", list.get(position).getId())
                        .putExtra("depositNum", list.get(position).getDepositNum()));
            }
        });
        type = getIntent().getStringExtra("type") + "";
        switch (type) {
            case "0":
                typeName = "有效徒弟";
                tv_name.setText("我的徒弟");
                tv_detail.setText("您现在还没有徒弟，赶快去收徒吧~");
                break;
            case "1":
                tv_name.setText("我的徒孙");
                typeName = "有效徒孙";
                tv_detail.setText("您现在还没有徒孙，赶快去收徒吧~");
                break;
        }
        new TitleBuilder(this).setTitleText(typeName)
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
        getList();
    }

    @OnClick({R.id.tv_share
    })
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_share://隐私协议
                finish();
                break;
        }
    }

    private void getList() {

        Task.getApiService().getAllIncomes().enqueue(new MyCallback<NewsBaseEntity>(NewsDDListActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    JSONArray jsonArray;
                    if ("0".equals(type)) {
                        jsonArray = jsonObject.getJSONArray("TuDi");
                    } else {
                        jsonArray = jsonObject.getJSONArray("TuSun");
                    }
                    if (jsonArray.size() > 0) {
                        ll_noRecord.setVisibility(View.GONE);
                        ll_title.setVisibility(View.VISIBLE);
                        recyclerView.setVisibility(View.VISIBLE);
                        for (int i = 0; i < jsonArray.size(); i++) {
                            NewsDDEntity newsDDEntity = new NewsDDEntity();
                            newsDDEntity.setId(JSON.parseObject(jsonArray.getString(i)).getInteger("userid") + "");
                            newsDDEntity.setImgUrl(JSON.parseObject(jsonArray.getString(i)).getString("avatar"));
                            newsDDEntity.setTime(JSON.parseObject(jsonArray.getString(i)).getString("createTime"));
                            newsDDEntity.setMoney1(JSON.parseObject(jsonArray.getString(i)).getInteger("discipleShareIncome") + "");
                            newsDDEntity.setMoney2(JSON.parseObject(jsonArray.getString(i)).getInteger("todyDiscipleShareIncome") + "");
                            newsDDEntity.setNikeName(JSON.parseObject(jsonArray.getString(i)).getString("nickname"));
                            newsDDEntity.setDepositNum(JSON.parseObject(jsonArray.getString(i)).getInteger("depositNum") + "");
                            list.add(newsDDEntity);
                        }
                        newsDDAdapter.notifyDataSetChanged();
                    } else {
                        ll_noRecord.setVisibility(View.VISIBLE);
                        ll_title.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });

    }
}
